package com.lanou.web.controller;

import com.lanou.entity.CookieUtils;
import com.lanou.entity.TbUser;
import com.lanou.entity.User;
import com.lanou.service.UserService;
import com.lanou.utils.ShiHuiResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//@Controller
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", required = true, value = "用户名"),
            @ApiImplicitParam(name = "password", required = true),
            @ApiImplicitParam(name = "email", required = true),
            @ApiImplicitParam(name = "phone", required = true)
    })
    @PostMapping("/user/showRegister")
    public Map<String, Object> register(String username,
                                        String password,
                                        String email,
                                        String phone) {
        TbUser tbUser = new TbUser();
        tbUser.setUsername(username);
        tbUser.setPassword(password);
        tbUser.setEmail(email);
        tbUser.setPhone(phone);

        boolean result = userService.register(tbUser);
        return ShiHuiResult.ok(result);
    }
    @Value("${TOKEN_KEY}")
    private String TOKEN_KEY;
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", required = true, value = "用户名"),
            @ApiImplicitParam(name = "password", required = true, value = "密码")
    })
    @PostMapping("/user/showLogin")
    public Map<String, Object> login(String username, String password,
                                     HttpServletRequest request, HttpServletResponse response){
         User result = userService.login(username,password);
        if (result != null){
//        CookieUtils.setCookie(request, response, TOKEN_KEY,result);
//            Cookie cookie = new Cookie("token", result);
//            cookie.setDomain("shihui.com");
//            cookie.setPath("/");
//            cookie.setMaxAge(1800);
//            cookie.setHttpOnly(true);
//            response.addCookie(cookie);
            return ShiHuiResult.ok(result);
        }else{
            return null;
        }
    }
    @ApiOperation("用户状态检查")
    @ApiImplicitParam(name = "token",required = true,value = "状态")
    @GetMapping("/user/{token}")
    public Map<String,Object> getUserByToken(@PathVariable("token")String token){
        TbUser user = userService.getUserByToken(token);
        if (user != null){
            return ShiHuiResult.ok(user);
        }else {
            return ShiHuiResult.error(500,"用户未登录");
        }
    }
    @ApiOperation("用户退出")
    @ApiImplicitParam(name = "token",required = true,value = "状态")
    @GetMapping("/user/out/{token}")
    public Map<String,Object> outUserByToken(@PathVariable("token")String token,HttpServletResponse response){
        TbUser user = userService.outUserByToken(token);
        if (user != null){
//            Cookie cookie = new Cookie("token", null);
//            cookie.setDomain("shihui.com");
//            cookie.setMaxAge(0);
//            cookie.setPath("/");
//            System.out.println("被删除的cookie名字为:"+cookie.getName());
//            response.addCookie(cookie);
            return ShiHuiResult.ok(user);
        }else {
            return ShiHuiResult.error(500,"用户未登录");
        }
    }

}
